local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);
local ReachList = nil
local Podium = workspace:WaitForChild("Podium")
local Players = game:GetService("Players")
local PrizeCFrames = {}
local PrizeAmplifies = {}
local AnimationList = {}
local AnimationTracks = {}
local winPlayerCloneModels = {}
local WinnerFolder = workspace:WaitForChild("WinnerModels")
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent)
	self.player = self.parent
	self:GetPodiumSetting()
	_G.server.GameStateChangeEvent:Connect(function(state)
        if state==2 then
			ReachList = self:GetReachPlayers()
			if ReachList then
				if #ReachList~=0 then
					print(ReachList)
					self:DestroyModels()
					self:CloneReachPlayers(ReachList)
					self:SetCloneModelPosition()
					self:PlayAnimation()
					self:GameEndTeleport()
				end
			end
		else
			self:clear()
        end
    end)
	
end;



--获取到达终点玩家list
function M.GetReachPlayers(self)
	local str = workspace:GetAttribute("ReachPlayers")
	local cleanedStr = string.gsub(str, "%[", "")  -- 去掉 "["
	cleanedStr = string.gsub(cleanedStr, "%]", "") -- 去掉 "]"
	local list = string.split(cleanedStr,",")
	for index, value in pairs(list) do
		list[index] = tonumber(value)
	end
	return list
end

--生成获奖玩家的替身
function M.CloneReachPlayers(self,reachList)
	for index, id in pairs(reachList) do
		local player = Players:GetPlayerByUserId(id)
		if player then
			player.Character.Archivable = true
			local cloneModel = player.Character:Clone()
			player.Character.Archivable =false
			cloneModel.Name = index
			local Humanoid = cloneModel:FindFirstChild("Humanoid")
			local trail = cloneModel.HumanoidRootPart.trail
			if trail then
				cloneModel.HumanoidRootPart.trail:Destroy()
			end
			if Humanoid then
				local scale = PrizeAmplifies[index]
				Humanoid.HeadScale.Value *=	scale
				Humanoid.BodyDepthScale.Value *= scale
				Humanoid.BodyWidthScale.Value *= scale
				Humanoid.BodyHeightScale.Value *= scale
			end
			cloneModel.Parent = WinnerFolder
			table.insert(winPlayerCloneModels,cloneModel)
		end
	end
end

function M.SetCloneModelPosition(self)
	for index, model in pairs(winPlayerCloneModels) do
		model:SetPrimaryPartCFrame(PrizeCFrames[index])
	end
end



--播放获胜动画
function M.PlayAnimation(self)
	for index, model in pairs(winPlayerCloneModels) do
		local Humanoid = model:WaitForChild("Humanoid")
		local animator =Humanoid:FindFirstChildOfClass("Animator")
		local animationTrack = animator:LoadAnimation(AnimationList[index])
		animationTrack.Looped = true
		animationTrack:Play()
		AnimationTracks[index] = animationTrack
	end
	print("播放领奖动画")
end

--初始化领奖台数据
function M.GetPodiumSetting(self)
	for index = 1, 3, 1 do
		PrizeCFrames[index] =  Podium.ctrl[index].value.WorldCFrame
		PrizeAmplifies[index] = Podium.ctrl[index].value.amplify.value
		AnimationList[index] = Podium.ctrl[index].value.dance
	end
	
end

function M.GameEndTeleport(self)
	self.GameEndPos = M.GetGameEndPos()
	self.GameEndPos:SetAttribute("isUsing",true)
	self.player.character:SetCFrame(self.GameEndPos.WorldCFrame)
	self:AddProcess(function()
		self:Wait(4)
		self.GameEndPos:SetAttribute("isUsing",false)
	end)
end

function M.GetGameEndPos()
    for _,v in pairs(workspace.GameEndPositions.ctrl:GetChildren()) do
		print("v:",v)
		print("v.value",v.value)
        if not v.value:GetAttribute("isUsing") then
            return v.value
        end
    end
end

function M.clear(self)
	ReachList = {}
end

function M.DestroyModels(self)
	for _, model in pairs(winPlayerCloneModels) do
		model:Destroy()
	end
	winPlayerCloneModels = {}
end

function M.dtor(self)
    M.super.dtor(self)
end

return M